package com.originskyseed.platform.user.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.PlayerRelateRecharge;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.PlayerRelateAnchorRecharge;
import com.originskyseed.platform.user.vo.request.PlayerRelateAnchorRechargeRequestVO;

/**
 * 玩家为主播累充信息表Controller类（OSI_PLAYER_RELATE_ANCHOR_RECHARGE）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/playerRelateAnchorRecharge")
@BaseLocaleResource(localeFileName = "i18n_user")
public class PlayerRelateAnchorRechargeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateAnchorRechargeController.class);

	@DubboReference
	private IPlayerRelateAnchorRechargeService playerRelateAnchorRechargeService;

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IPlayerRelateConsumeMonthService playerRelateConsumeMonthService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateAnchorRechargeList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody playerRelateAnchorRechargeList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			PlayerRelateAnchorRechargeRequestVO requestVO = this.getParameterBean( PlayerRelateAnchorRechargeRequestVO.class);
			// 查询优质玩家
			requestVO.setIsHighQuality(1);

			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();

			// 分页查询
			BasePage<PlayerRelateAnchorRecharge> page = new BasePage<PlayerRelateAnchorRecharge>(requestVO.getPageNo(), requestVO.getPageSize());
			// 根据范围查询
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				List<String> otherAccountList = new ArrayList<>();
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					if (dept != null && dept.getCode() != null) {
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 1);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {
					return new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
				}
				requestVO.setList(otherAccountList);
			}

			page = this.playerRelateAnchorRechargeService.selectListByPage(requestVO.toMap(), page);
			// 查询当日消费金额
			Map<String,Object> map = new HashMap<>();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = simpleDateFormat.format(new Date());
			map.put("startTime",today);
			map.put("endTime",today);
			for (PlayerRelateAnchorRecharge playerRelateAnchorRecharge:page.getResults()) {
				map.put("anchorId",playerRelateAnchorRecharge.getAnchorId());
				BigDecimal todayAmount = this.playerRelateConsumeMonthService.selectTotalAmountByMap(map);
				playerRelateAnchorRecharge.setTodayAmount(todayAmount);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateAnchorRechargeSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<PlayerRelateAnchorRecharge> playerRelateAnchorRechargeSave(HttpServletRequest request) {
		BaseResponseBody<PlayerRelateAnchorRecharge> result = null;
		try {
			// 请求对象
			PlayerRelateAnchorRecharge playerRelateAnchorRecharge = this.getParameterBean(PlayerRelateAnchorRecharge.class);

			if (StringUtils.isNotBlank(playerRelateAnchorRecharge.getId())) {
				this.playerRelateAnchorRechargeService.updateByPrimaryKey(playerRelateAnchorRecharge);
			} else {
				this.playerRelateAnchorRechargeService.insertSelective(playerRelateAnchorRecharge);
			}
			result = new BaseResponseBody<PlayerRelateAnchorRecharge>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), playerRelateAnchorRecharge);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<PlayerRelateAnchorRecharge>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/playerRelateAnchorRechargeDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> playerRelateAnchorRechargeDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.playerRelateAnchorRechargeService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 查询组织下的部门
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}
}